package cn.sirun.com.fca.personal.center.view.impl;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.tencent.cloud.iov.common.flow.view.impl.BaseLoadingFlowView;
import com.tencent.cloud.iov.flow.presenter.IPresenter;
import com.tencent.cloud.iov.flow.view.IContentViewContainer;
import com.tencent.cloud.iov.kernel.constant.StatusCode;
import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.util.ResourcesUtils;
import com.tencent.cloud.uikit.ui.widget.IovToolbar;
import com.tencent.cloud.uikit.utils.DialogUtils;
import com.tencent.cloud.uikit.utils.SystemUiUtils;
import com.tencent.cloud.uikit.widget.edittext.LineEditText;

import cn.sirun.com.fca.account.constant.CarConst;
import cn.sirun.com.fca.personal.center.R;
import cn.sirun.com.fca.personal.center.model.request.SubmitPlateNumberRequest;
import cn.sirun.com.fca.personal.center.presenter.ICarManagerPresenter;
import cn.sirun.com.fca.personal.center.presenter.impl.CarManagerPresenter;
import cn.sirun.com.fca.personal.center.view.ISettingCarNumView;

public class SettingCarNumFlowView extends BaseLoadingFlowView<ICarManagerPresenter>
        implements ISettingCarNumView {

    private LineEditText mNumET;
    private String mCarDin;
    private String mPlateNumber;

    public SettingCarNumFlowView(Activity activity) {
        super(activity);
    }

    public SettingCarNumFlowView(Activity activity, IContentViewContainer container) {
        super(activity, container);
    }

    @Override
    public void onEditPlateNumberResult(BaseResponse<Void> response) {
        hideLoadingView();
        int code = response.getCode();
        if (code == StatusCode.SUCCESS) {
            Intent intent = new Intent();
            intent.putExtra(CarConst.SpKey.CAR_PLATE_NUMBER, mPlateNumber);
            getActivity().setResult(CarConst.SETTING_CAR_PLATE_NUMBER_RESULT, intent);
            getActivity().finish();
            return;
        }
        DialogUtils.showToast(getActivity(), response.getMsg());
    }

    @Override
    protected ICarManagerPresenter createPresenter() {
        return new CarManagerPresenter();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        setUpActionBar();
        initView();
    }

    private void setUpActionBar() {
        final Activity activity = getActivity();

        final IovToolbar toolbar = SystemUiUtils.showActionBar(
                activity, ResourcesUtils.getString(R.string.setting_car_num), ResourcesUtils.getDrawable(R.color.white));
        toolbar.getmBackImageView().setVisibility(View.GONE);
        toolbar.getmBackTextView().setTextColor(ResourcesUtils.getColor(R.color.grey_A9));
        toolbar.setHomeAsUpText(R.string.cancel);
        toolbar.getmMenuTextView().setTextColor(ResourcesUtils.getColor(R.color.blue_5C));
        toolbar.setMenu(R.string.title_complete, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handleSureClick();
            }
        });
    }

    private void handleSureClick() {
        mPlateNumber = mNumET.getText().toString().trim();
        if (!TextUtils.isEmpty(mPlateNumber)) {
            showLoadingView();
            SubmitPlateNumberRequest request = new SubmitPlateNumberRequest(mCarDin, mPlateNumber);
            getPresenter().editCarNumber(request);
        }
    }

    private void initView() {
        setContentView(R.layout.setting_car_num_activity);
        final Activity activity = getActivity();
        mNumET = activity.findViewById(R.id.et_car_num);
        initData();
    }

    private void initData() {
        mPlateNumber = getActivity().getIntent().getStringExtra(CarConst.SpKey.CAR_PLATE_NUMBER);
        mCarDin = getActivity().getIntent().getStringExtra(CarConst.SpKey.CAR_ACTIVATE_INFO);
        if (!TextUtils.isEmpty(mPlateNumber)) {
            mNumET.setText(mPlateNumber);
        }
    }

}
